package clinivida.DAO;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;

import clinivida.entity.Cidade;
import clinivida.entity.Consulta;
import clinivida.entity.Endereco;
import clinivida.entity.Especialidade;
import clinivida.entity.Estado;
import clinivida.entity.Estadocivil;
import clinivida.entity.Medico;
import clinivida.entity.Pais;
import clinivida.entity.Pessoa;
import clinivida.entity.Usuario;

public class HibernateUtil
{
    
    private static HibernateUtil me;
    
    private SessionFactory       sessionFactory;
    
    public HibernateUtil()
    {
        sessionFactory = (SessionFactory) new AnnotationConfiguration()
        .setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver")
        .setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/clinivida")
        .setProperty("hibernate.connection.username", "root")
        .setProperty("hibernate.connection.password", "gaspar")
        .setProperty("hibernate.c3p0.arquire_increment", "1")
        .setProperty("hibernate.c3p0.idle_test_period", "100")
        .setProperty("hibernate.c3p0.min_size", "5")
        .setProperty("hibernate.c3p0.max_size", "20")
        .setProperty("hibernate.c3p0.timeout", "1800")
        .setProperty("hibernate.c3p0.max_statements", "50")
        .setProperty("hibernate.dialect","org.hibernate.dialect.MySQLDialect")
        .setProperty("hibernate.hbm2ddl.auto", "none")
        .setProperty("hibernate.show_sql", "true")
        .setProperty("hibernate.format_sql", "true")
        .addAnnotatedClass(Usuario.class)
        .addAnnotatedClass(Pessoa.class)
        .addAnnotatedClass(Medico.class)
        .addAnnotatedClass(Estadocivil.class)
        .addAnnotatedClass(Estado.class)
        .addAnnotatedClass(Especialidade.class)
        .addAnnotatedClass(Consulta.class)
        .addAnnotatedClass(Cidade.class)
        .addAnnotatedClass(Endereco.class)
        .addAnnotatedClass(Pais.class)
        .buildSessionFactory();
    }
    
    public Session getSession(){
        Session toReturn = sessionFactory.openSession();
        toReturn.beginTransaction();
        return toReturn;
    }
    
    public static HibernateUtil getInstance()
    {
        if(me == null)
        {
            me = new HibernateUtil();
        }
        return me;
    }
 }
